package com.fms.kccc.adapter;

import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.fms.kccc.R;
import com.fms.kccc.library.ImageViewFullWidth;
import com.fms.kccc.object.ImageContent;
import com.fms.kccc.util.Common;

public class ImageListViewAdapter extends ArrayAdapter<ImageContent> {

	/**
	 * ViewHolder class for layout.<br />
	 * <br />
	 * Auto-created on 2013-07-28 11:20:36 by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 */
	private static class ViewHolder {
		public final LinearLayout rootView;
		public final TextView tvTitle;
		public final TextView tvSource;
		public final ImageViewFullWidth ivImage;

		private ViewHolder(LinearLayout rootView, TextView tvTitle,
				TextView tvSource, ImageViewFullWidth ivImage) {
			this.rootView = rootView;
			this.tvTitle = tvTitle;
			this.tvSource = tvSource;
			this.ivImage = ivImage;
		}

		public static ViewHolder create(LinearLayout rootView) {
			TextView tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
			TextView tvSource = (TextView) rootView
					.findViewById(R.id.tv_source);
			ImageViewFullWidth ivImage = (ImageViewFullWidth) rootView
					.findViewById(R.id.iv_image);
			return new ViewHolder(rootView, tvTitle, tvSource, ivImage);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder vh;
		if (convertView == null) {
			View view = mInflater
					.inflate(R.layout.lv_item_image, parent, false);
			vh = ViewHolder.create((LinearLayout) view);
			view.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final ImageContent item = getItem(position);
		final AQuery aq = aQuery.recycle(convertView);
		String imageURL = isUseImageBackup ? getURLBackup(item.getImageURL())
				: item.getSmallImageURL();
		aq.id(vh.ivImage).image(imageURL, memCache, fileCache,
				vh.ivImage.getMeasuredHeight(), R.drawable.error,
				new BitmapAjaxCallback() {

					@Override
					public void callback(String url, ImageView iv, Bitmap bm,
							AjaxStatus status) {
						if (bm == null) {
							aq.id(vh.ivImage).image(
									getURLBackup(item.getImageURL()), memCache,
									fileCache, vh.ivImage.getMeasuredHeight(),
									R.drawable.error);
						} else {
							iv.setImageBitmap(bm);
						}
					}

				});
		vh.tvTitle.setText(item.getTitle());
		vh.tvSource.setText(item.getSource());
		return vh.rootView;
	}

	private LayoutInflater mInflater;
	private boolean isUseImageBackup = false;
	private AQuery aQuery;
	private boolean memCache = true;
	private boolean fileCache = true;

	// Constructors
	public ImageListViewAdapter(Context context, List<ImageContent> objects) {
		super(context, 0, objects);
		this.mInflater = LayoutInflater.from(context);
		aQuery = new AQuery(context);
	}

	public String getURLBackup(String url) {
		return String.format(Common.RequestURL.GET_BACKUP_IMAGE, url);
	}
}
